<?php
    class ApiCenter_VendorsApi_Model_Warehouse_Api_V2 extends  ApiCenter_VendorsApi_Model_Warehouse_Api {
            
        
        
        /**
         * 添加仓库
         */
        
        public function  uploadWarehouse($data,$login){
             $vendor = "";
            $login = json_decode($login,true);
            $data = json_decode($data,true);
            try {
               if(is_array($login)){
                   $vendor =  Mage::helper("vendorsapi")->validateVendors($login['username'],$login['password']);
                   $this->_tocheckFiled($data);
                   $this->_toSaveData($data, $vendor);
               }
               $result['status'] = "success";
               $result['info'] = "仓库创建成功！";
            } catch (Exception $e) {
                $result['status'] = "error";
                $result['info'] = $e->getMessage();
            }
            return json_encode($result);
            
        }
        
        
        
        /**
         * 查询仓库信息
         */
        public  function queryWarehouse($login){
            $vendor = "";
            $login = json_decode($login,true);
            try {
               if(is_array($login)){
                    $vendor =  Mage::helper("vendorsapi")->validateVendors($login['username'],$login['password']);
                    $collection =   Mage::getResourceModel("vendorswarehouse/address_collection");

                    $collection->addFieldToFilter("main_table.vendor_id",$vendor->getId());
                    $collection->addFieldToSelect("sender_name");
                    $collection->addFieldToSelect("sender_zip_code");
                    $collection->addFieldToSelect("paltform");      
                    $collection->getSelect()
                    ->join(array("d"=>"ves_vendor_warehouse_details"),
                        "main_table.entity_id=d.wid",
                        array(
                            "sender_phone"=>"sender_phone",
                            "sender_country"=>"sender_country",
                            "sender_province"=>"sender_province",
                            "sender_city"=>"sender_city",
                            "sender_postcode"=>"sender_postcode",
                            "sender_address"=>"sender_address",
                            "sender_contact"=>"sender_contact",
                            "sender_company"=>"sender_company",
                            "sender_email"=>"sender_email",
                            "created_time"=>"created_time",
                        )
                    );
                    $default_code = $vendor->getWarehouseCode();
                    $data = $collection->getItems();
                    if(empty($data)){
                        $result['status'] = "success";
                        $result['info'] = "未查询到仓库信息，请先创建，否则将无法创建产品！";
                        return json_encode($result);
                    }
                    foreach ($collection->getItems() as $k=>$warehouse) {
                      
                        if($warehouse->getSenderZipCode()==$default_code){
                            $info = $warehouse;
                        }
                    }
               }else{
                   $result['status'] = "error";
                   $result['info'] = "商家账号信息有误！";
                     return json_encode($result);
               }
               $result['status'] = "success";
               $result['info'] = $info->toArray();
            } catch (Exception $e) {
                $result['status'] = "error";
                $result['info'] = $e->getMessage();
            }
            return json_encode($result);
            

        }
        
    }